package info.qingshui.browser;

import java.util.ArrayList;
import java.util.List;

import info.qingshui.browser.R;
import info.qingshui.browser.hk.HKActivity;
import info.qingshui.browser.hk.UpdateManager;

import info.qingshui.browser.hk.setting.Setting;
import android.app.TabActivity;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TabHost;
import android.widget.TabWidget;
import android.widget.TextView;

public class Main extends TabActivity {


	private TabHost tabHost;

	
	

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		boolean needUpdate = false;
		if( !this.getBaseContext().getPackageName().equals("info.qingshui.browser") ) {
			//当前不是以这个包名运行
			if(isAvilible(this, "info.qingshui.browser")){ 
	            Intent i = new Intent(); 
	            ComponentName cn = new ComponentName("info.qingshui.browser",  "info.qingshui.browser.Main"); 
	            i.setComponent(cn); 
	            this.startActivityForResult(i, 0); 
	            this.finish();
	            return;
	        } 	   
		    else {  //未安装，跳转至market下载该程序 或者提示更新
		    	//提示更新
		    	needUpdate = true;
//	          Uri uri = Uri.parse("market://details?id=info.qingshui.browser");//id为包名 
//	          Intent it = new Intent(Intent.ACTION_VIEW, uri); 
//	          startActivity(it); 
	          
	      
		    } 
		    
		}
		
		
		setContentView(R.layout.main);
		tabHost = getTabHost();

		Intent intent1 = new Intent(this, China.class);
		createTab( getResources().getString(R.string.main_china), intent1);
		

		Intent intent2 = new Intent(this, Hongkong.class);
		createTab(getResources().getString(R.string.main_hongkong), intent2);

		Intent intent3 = new Intent(this, Taiwan.class);
		createTab(getResources().getString(R.string.main_taiwan), intent3);

		Intent intent4 = new Intent(this, Haiwai.class);
		createTab(getResources().getString(R.string.main_haiwai), intent4);

		Intent intent5 = new Intent(this, Others.class);
		createTab(getResources().getString(R.string.main_others), intent5);
		
		TabWidget tabWidget = tabHost.getTabWidget();
		int count = tabWidget.getChildCount();

		DisplayMetrics dm = new DisplayMetrics();
		getWindowManager().getDefaultDisplay().getMetrics(dm);

		// 窗口的宽度
		int screenWidth = dm.widthPixels;
		//Log.i("test", "screenWidth=" + screenWidth);
		if (count > 6) {
			for (int i = 0; i < count; i++) {
				tabWidget.getChildTabViewAt(i).setMinimumWidth(screenWidth / 5);
			}
		}
		tabHost.setCurrentTab(1);
		
		//判断当前是否需要升级
		Setting setting = this.getBrowserApplication().getSetting();
		if (needUpdate) {
			UpdateManager updateManager = new UpdateManager(null, Main.this ,"当前版本需要更新才能使用","http://12315bbs.com/4.apk" );
			updateManager.setCanCancel( false );
			updateManager.checkUpdateInfo();
			
		}
		
		else if( setting.isNeedUpdateVersion() && !setting.isRemaindedUpdate()) {
			//setting.setRemaindedUpdate(true);
			UpdateManager updateManager = new UpdateManager(setting, Main.this ,setting.getUpdateDesc(), setting.getUpdateVersionUrl() );
			updateManager.checkUpdateInfo();
			
		}
	}

	
	protected void onResume() {
		super.onResume();
		//判断主浏览器是否已经打开的，如果是，放最前面
		
//		if( this.getBrowserApplication().getHKActivity() != null ){
//			Log.d(Main.class.getSimpleName(), "main onResume " + this.getBrowserApplication().getHKActivity());
//			// 通过intent跳转到下一个页面.
//			Intent intent = new Intent();
//			intent.setClass(this.getBaseContext(), HKActivity.class);
//			// 通过Bundle来获取数据,通过key-Value的方式放入数据
//			Bundle bl = new Bundle();
////			bl.putString("go_url", item.getUrl());
////			// 将Bundle放入Intent传入下一个Activity
////			intent.putExtras(bl);
////			// 跳到下一个Activity,并且等待其返回结果
////			this.star
//			this.startActivity(intent);
//		}
		// WebView.enablePlatformNotifications();//试用代理时用


		Log.d(Main.class.getSimpleName(), "main onResume");
	}
	
	private void createTab(String text, Intent intent) {
		tabHost.addTab(tabHost.newTabSpec(text).setIndicator(
				createTabView(text)).setContent(intent));
	}

	private View createTabView(String text) {
		View view = LayoutInflater.from(this).inflate(R.layout.tab_indicator,
				null);
		TextView tv = (TextView) view.findViewById(R.id.tv_tab);
		tv.setText(text);
		return view;
	}

	
	public BrowserApplication getBrowserApplication() {
		return (BrowserApplication)super.getApplication();
	
	}
	
	//下面介绍怎么判断手机已安装某程序的方法： 
	private boolean isAvilible(Context context, String packageName){ 
	          final PackageManager packageManager = context.getPackageManager();//获取packagemanager 
	          List< PackageInfo> pinfo = packageManager.getInstalledPackages(0);//获取所有已安装程序的包信息 
	          List<String> pName = new ArrayList<String>();//用于存储所有已安装程序的包名 
	         //从pinfo中将包名字逐一取出，压入pName list中 
	              if(pinfo != null){ 
	              for(int i = 0; i < pinfo.size(); i++){ 
	                  String pn = pinfo.get(i).packageName; 
	                  pName.add(pn); 
	              } 
	          } 
	          return pName.contains(packageName);//判断pName中是否有目标程序的包名，有TRUE，没有FALSE 
	 } 
	
	
}